/* 
 *  <copyright file="RemoveOnLastFrameAnimTex.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;

using Microsoft.Xna.Framework;

using SobrietyEngine.Assets.Textures;

namespace SobrietyEngine.Components.Graphic
{
    public class RemoveOnLastFrameAnimTex : AnimatedSpriteCmp, SobrietyEngine.Components.IComponent
    {
        public RemoveOnLastFrameAnimTex()
            : base()
        {
        }

         override public Boolean InitComponent()
        {
            base.InitComponent();
            return true;
        }

        public new void Update(GameTime elapsedTime)
        {
            nextUpdate -= (elapsedTime.ElapsedGameTime.Milliseconds / 1000.0f) * animationSpeedMult;
            while (nextUpdate <= 0)
            {
                if (Reverse)
                    ++currentFrame;
                else
                    --currentFrame;
                if (currentFrame >= currentFrames.Count)
                {
                    //end anim
                    Actor.MarkedForRemoval = true;
                    currentFrame = 0;
                }
                else if (currentFrame < 0)
                {
                    //end of single anim
                    Actor.MarkedForRemoval = true;
                    currentFrame = currentFrames.Count - 1;
                }

                nextUpdate += ((FrameData)currentFrames[currentFrame]).Time;
            }


            spriteTexture = ((FrameData)currentFrames[currentFrame]).Texture;

            if (spriteTexture.Width > spriteTexture.Height)
                boundingRadius = spriteTexture.Width;
            else
                boundingRadius = spriteTexture.Height;
        }

    }
}